home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Washington_1988 / DevCon88.2 / StartingAmigaC / exam5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  17.1 KB  |  595 lines

  1. /*
  2.    Example of opening a simple window on a custom screen - #5
  3.    written by Dan Schein April-88 for the 1988 AMIGA Developers Conference. 
  4.  
  5.    This example, and all those based on this example, were compiled and
  6.    tested with the Lattice V4.0 compiler.
  7. */
  8.  
  9. /*
  10.   Copyright (c) 1988 Commodore-Amiga, Inc.
  11.  
  12.   Executables based on this information may be used in software
  13.   for Commodore Amiga computers.  All other rights reserved.
  14.  
  15.   This information is provided "as is"; no warranties are made.
  16.   All use is at your own risk, and no liability or responsibility is assumed.
  17. */
  18.  
  19.  
  20. #include <exec/types.h>
  21. #include <intuition/intuition.h>
  22. #include <graphics/display.h>
  23. #include <libraries/dos.h>
  24. #include <stdio.h>
  25.  
  26. /*
  27.    Define the Version of the Amiga OS we want (require)
  28.  
  29.    Operating System Version Information
  30.  
  31.       0     Any version is OK
  32.       30    1.0 or higher
  33.       31    1.1 NTSC or Higher
  34.       32    1.1 PAL  or Higher
  35.       33    1.2 or higher
  36.       34    1.3 or higher
  37.  
  38. */
  39.  
  40. #define INTUITION_REV 0
  41. #define GRAPHICS_REV  0
  42.  
  43. /*
  44.    Define the Flags for use in our menu structure's - Refer to the Intuition
  45.    Reference Manual for more detail
  46. */
  47.  
  48. #define FLAGSA ITEMTEXT|MENUTOGGLE|ITEMENABLED
  49. #define FLAGSB CHECKIT|ITEMTEXT|MENUTOGGLE|ITEMENABLED|COMMSEQ
  50. #define FLAGSC ITEMTEXT|MENUTOGGLE|ITEMENABLED|COMMSEQ
  51.  
  52. BOOL  Done;
  53. ULONG WakeUpBits, class, code, selection, menuNum, itemNum;
  54.  
  55. struct TextAttr MyFont =
  56. {
  57.    "topaz.font",                             /* Font Name                 */
  58.    TOPAZ_EIGHTY,                             /* Font Height               */
  59.    FS_NORMAL,                                /* Style                     */
  60.    FPF_ROMFONT                               /* Preferences               */
  61. };
  62.  
  63. struct NewScreen OurScreen =
  64. {
  65.    0, 0,                                     /* LeftEdge and TopEdge     */
  66.    640, 200,                                 /* Width and Height         */
  67.    2,                                        /* Depth (4 colors)         */
  68.    0, 1,                                     /* DetailPen and BlockPen   */
  69.    HIRES,                                    /* Special Display Mode     */
  70.    CUSTOMSCREEN,                             /* The Screen Type          */
  71.    &MyFont,                                  /* Use our own Font         */
  72.    "A Simple Screen",                        /* Screen Title             */
  73.    NULL,                                     /* Special Screen Gadgets   */
  74.    NULL                                      /* Special CustomBitMap     */
  75. };
  76.  
  77. struct NewWindow OurWindow =
  78. {
  79.    20, 20,                                   /* LeftEdge and TopEdge      */
  80.    400, 160,                                 /* Width and Height          */
  81.    0, 1,                                     /* DetailPen and BlockPen    */
  82.    CLOSEWINDOW|MENUPICK,                     /* IDCMP Flags               */
  83.    WINDOWCLOSE|SMART_REFRESH|                /* Flags                     */
  84.    ACTIVATE|WINDOWDRAG|WINDOWDEPTH|
  85.    WINDOWSIZING|SMART_REFRESH,
  86.    NULL, NULL,                               /* Gadget and Image pointers */
  87.    "A Simple Window",                        /* Window Title              */
  88.    NULL,                                     /* Screen pointer            */
  89.    NULL,                                     /* BitMap pointer            */
  90.    100, 25,                                  /* MinWidth and MinHeight    */
  91.    500, 180,                                 /* MaxWidth and MaxHeight    */
  92.    CUSTOMSCREEN                              /* Type of window            */
  93. };
  94.  
  95. struct IntuiText IText[] =
  96. {
  97.              /* Breakdown of the IntuiText Structure */
  98.    {
  99.    0, 1,                                     /* FrontPen and BackPen      */
  100.    JAM2,                                     /* DrawMode                  */
  101.    CHECKWIDTH, 0,                            /* LeftEdge and TopEdge      */
  102.    NULL,                                     /* TextAttr                  */
  103.    "QUIT?",                                  /* Itext (text)              */
  104.    NULL                                      /* NextText                  */
  105.    },
  106.              /* Most commonly used IntuiText syntax  */
  107.  
  108.    { 0, 1, JAM2, CHECKWIDTH, 0, NULL, "SURE!",   NULL},
  109.    { 0, 1, JAM2, CHECKWIDTH, 0, NULL, "FLASHER", NULL},
  110.    { 0, 1, JAM2, CHECKWIDTH, 0, NULL, "FLASH",   NULL},
  111.    { 0, 1, JAM2, CHECKWIDTH, 0, NULL, "ON/OFF",  NULL},
  112.    { 0, 1, JAM2, CHECKWIDTH, 0, NULL, "FLIP?",   NULL}
  113. };
  114.  
  115. struct MenuItem M0I1S0[] =                   /* This is Menu 0 - Item 1   */
  116. {                                            /*               SubItem 0   */
  117.    {
  118.    NULL,                                     /* Next Item                 */
  119.    100, 6,                                   /* LeftEdge and TopEdge      */
  120.    80+CHECKWIDTH, 10,                        /* Width and Height          */
  121.    FLAGSC,                                   /* Flags (see #define)       */
  122.    0,                                        /* Mutual Exclude            */
  123.    (APTR)&IText[3],                          /* ItemFill                  */
  124.    NULL,                                     /* SelectFill                */
  125.    'F',                                      /* Command                   */
  126.    NULL                                      /* NextSelect                */
  127.    } 
  128. };
  129.  
  130. struct MenuItem M0I2S0[] =                   /* This is Menu 0 - Item 2   */
  131. {                                            /*               SubItem 0   */
  132.    {
  133.    NULL,                                     /* Next Item                 */
  134.    100,  6,                                  /* LeftEdge and TopEdge      */
  135.    80+CHECKWIDTH, 10,                        /* Width and Height          */
  136.    FLAGSB,                                   /* Flags (see #define)       */
  137.    0,                                        /* Mutual Exclude            */
  138.    (APTR)&IText[5],                          /* ItemFill                  */
  139.    NULL,                                     /* SelectFill                */
  140.    'R',                                      /* Command                   */
  141.    NULL                                      /* NextSelect                */
  142.    } 
  143. };
  144.  
  145. struct MenuItem M0[] = 
  146. {
  147.    {                                         /* This is Menu 0 - Item 0   */
  148.    &M0[1],                                   /* Next Item                 */
  149.    0, 0,                                     /* LeftEdge and TopEdge      */
  150.    56+CHECKWIDTH, 10,                        /* Width and Height          */
  151.    FLAGSA,                                   /* Flags (see #define)       */
  152.    0,                                        /* Mutual Exclude            */
  153.    (APTR)&IText[0],                          /* ItemFill (text)           */
  154.    (APTR)&IText[1],                          /* SelectFill (alt-text)     */
  155.    NULL,                                     /* Command                   */
  156.    NULL,                                     /* SubItem                   */
  157.    NULL                                      /* NextSelect                */
  158.    },
  159.  
  160.    {                                         /* This is Menu 0 - Item 1   */
  161.    &M0[2],                                   /* Next Item                 */
  162.    0, 12,                                    /* LeftEdge and TopEdge      */
  163.    56+CHECKWIDTH, 10,                        /* Width and Height          */
  164.    FLAGSA,                                   /* Flags (see #define)       */
  165.    0,                                        /* Mutual Exclude            */
  166.    (APTR)&IText[2],                          /* ItemFill (text)           */
  167.    NULL,                                     /* SelectFill (alt-text)     */
  168.    NULL,                                     /* Command                   */
  169.    &M0I1S0[0],                               /* SubItem                   */
  170.    NULL                                      /* NextSelect                */
  171.    },
  172.  
  173.    {                                         /* This is Menu 0 - Item 2   */
  174.    NULL,                                     /* Next Item                 */
  175.    0, 24,                                    /* LeftEdge and TopEdge      */
  176.    56+CHECKWIDTH, 10,                        /* Width and Height          */
  177.    FLAGSA,                                   /* Flags (see #define)       */
  178.    0,                                        /* Mutual Exclude            */
  179.    (APTR)&IText[4],                          /* ItemFill (text)           */
  180.    NULL,                                     /* SelectFill (alt-text)     */
  181.    NULL,                                     /* Command                   */
  182.    &M0I2S0[0],                               /* SubItem                   */
  183.    NULL                                      /* NextSelect                */
  184.    } 
  185. };
  186.  
  187. struct Menu TheMenu[] =
  188. {
  189.    {
  190.    NULL,                                     /* Next Menu                 */
  191.    0, 0,                                     /* LeftEdge and TopEdge      */
  192.    56+CHECKWIDTH, 10,                        /* Width and Height          */
  193.    MENUENABLED,                              /* Flags                     */
  194.    "OUR MENU",                               /* Menu Title                */
  195.    &M0[0]                                    /* First Item                */
  196.    } 
  197. };
  198.  
  199. struct Screen *screen;
  200. struct Window *window;
  201. struct GfxBase *GfxBase;
  202. struct IntuiMessage *message;
  203. struct Menu *MyMenu = &TheMenu[0];
  204. struct IntuitionBase *IntuitionBase;
  205.  
  206. /*
  207.    The following items have been added for use by the console IO routines 
  208. */
  209.  
  210. UBYTE inKey;
  211. ULONG  conSigMask;
  212. #define SBUFSZ  (128)
  213. extern struct MsgPort *cmPort;
  214. int   scnt, OpenedCon = 0;
  215. char  *sbp, sbuf[SBUFSZ];
  216.  
  217. void main(), chkmsg(), flash(), cleanexit(), cleanup();
  218. void chkkey(), addchar(), backspace(), escape(), CDClose();
  219. void enter(), queue_read(), CDPutChar(), CDPutStr(), CDPutStrL();
  220.  
  221.  
  222.  
  223. void main()
  224. {
  225.               /* Method #1 of opening and error testing */
  226.  
  227.    IntuitionBase=(struct IntuitionBase*)
  228.       OpenLibrary("intuition.library",INTUITION_REV);
  229.  
  230.    if (IntuitionBase == NULL)
  231.    {
  232.       cleanexit ("Open Intuition Library failed!\n", RETURN_FAIL);
  233.    }
  234.  
  235.    GfxBase=(struct GfxBase*)OpenLibrary("graphics.library",GRAPHICS_REV);
  236.  
  237.    if (GfxBase == NULL)
  238.    {
  239.       cleanexit ("Open Graphics Library failed!\n", RETURN_FAIL);
  240.    }
  241.  
  242.               /* Method #2 of opening and error testing */
  243.  
  244.    if((screen=(struct Screen*)OpenScreen(&OurScreen))==NULL)
  245.    {
  246.       cleanexit ("Open Screen failed!\n", RETURN_FAIL);
  247.    }
  248.  
  249.    OurWindow.Screen=screen;
  250.  
  251.    if((window=(struct Window *)OpenWindow(&OurWindow))==NULL)
  252.    {
  253.       cleanexit ("Open Window failed!\n", RETURN_FAIL);
  254.    }
  255.  
  256.    SetMenuStrip(window, MyMenu);
  257.  
  258.    Move(window->RPort, 20, 20);
  259.    Text(window->RPort, "Our own TEXT string!", 20);
  260.    Delay(50);
  261.  
  262.    if(!(OpenedCon=CDOpen(window)))
  263.       {
  264.       cleanexit ("Open Console Device Error!\n", RETURN_FAIL);
  265.       }
  266.  
  267.    CDPutStr("This string output to console device\n");
  268.  
  269.    sbp = sbuf;
  270.    *sbp = '\0';
  271.    scnt = 0;
  272.    Done = FALSE;
  273.  
  274.    while(!Done)
  275.    {
  276.       WakeUpBits = Wait((1<<window->UserPort->mp_SigBit) |
  277.                        (1<<cmPort->mp_SigBit));
  278.  
  279.       if(WakeUpBits & (1<<window->UserPort->mp_SigBit))
  280.       {
  281.          chkmsg();
  282.       }
  283.  
  284.       if(WakeUpBits & (1<<cmPort->mp_SigBit))
  285.       {
  286.          chkkey();
  287.       }
  288.  
  289.    }
  290.  
  291.    cleanup();
  292.    exit (RETURN_OK);
  293. }
  294.  
  295.  
  296.  
  297. void chkkey()
  298. {
  299. int i;
  300. UBYTE c;
  301.  
  302.    while((i=CDMayGetChar()) != -1)
  303.    {
  304.       c = (UBYTE)i;
  305.       if(c == 0x08) backspace();
  306.       else if((c == 0x1b)||(c == 0x9b)) escape();
  307.       else if(c == 0x0d) enter();
  308.       else if(((c>=0x20)&&(c<=0x7e)) || (c>=0xa0))  /* printable */
  309.       {
  310.          inKey = c;
  311.          addchar(c);
  312.       }
  313.    }
  314. }
  315.  
  316. void addchar(c)
  317. UBYTE c;
  318. {
  319.    if((scnt<SBUFSZ-2) || ((scnt<SBUFSZ-1)&&(c='\n')))
  320.    {
  321.       *sbp++ = c;
  322.       scnt++;
  323.       CDPutChar(c);
  324.    }
  325. }
  326.  
  327. void backspace()
  328. {
  329.    if (scnt>0)
  330.    {
  331.       CDPutStr("\b \b");
  332.       *sbp-- = (' ');
  333.       scnt--;
  334.    }
  335. }
  336.  
  337. void escape()
  338. {
  339. UBYTE c;
  340.    /*
  341.       We'll just discard ESC sequences (cursors, help, etc) in this example
  342.       You could parse them and act on them
  343.    */
  344.    do
  345.    {
  346.       c = CDGetChar();
  347.    }
  348.    while((c == '[')||(c < 0x40)||(c > 0x7e));
  349. }
  350.  
  351.  
  352. void enter()
  353. {
  354.    *sbp = '\0';
  355.    CDPutChar('\n');
  356.  
  357.    CDPutStr("Buffer contains:\n");
  358.    CDPutStr(sbuf);
  359.    CDPutChar('\n');
  360.  
  361.    sbp = sbuf;
  362.    *sbp = '\0';
  363.    scnt = inKey = 0;
  364. }
  365.  
  366. void chkmsg()
  367. {
  368.    while(message = (struct IntuiMessage *)GetMsg(window->UserPort))
  369.    {
  370.       class = message->Class;
  371.       code  = message->Code;
  372.       switch(class)
  373.       {
  374.          case CLOSEWINDOW:
  375.             Done = TRUE;
  376.             break;
  377.          case MENUPICK:
  378.             selection = code;
  379.             while (selection != MENUNULL)
  380.             {
  381.                menuNum = MENUNUM(selection);
  382.                itemNum = ITEMNUM(selection);
  383.                switch(menuNum)
  384.                {
  385.                   case 0:    /* Project Menu */
  386.                   switch(itemNum)
  387.                   {
  388.                      case 0:
  389.                         Done = TRUE;
  390.                         break;
  391.                      case 1:
  392.                         flash();
  393.                         break;
  394.                      case 2:
  395.                         break;
  396.                      default:
  397.                         break;
  398.                   }
  399.                   break;
  400.  
  401.                   default:
  402.                      break;
  403.                }
  404.                selection = ((struct MenuItem *)ItemAddress
  405.                      (&TheMenu,selection))->NextSelect;
  406.             }
  407.             break;
  408.          default:
  409.             break;
  410.       }
  411.    ReplyMsg(message);
  412.    }
  413. }
  414.  
  415. void flash()
  416. {
  417.    int i;
  418.  
  419.    for (i=0;i<4;i++)      /* do it four times  */
  420.    {
  421.       DisplayBeep(0);     /* screen flash!     */
  422.       Delay (15);         /* wait 15/50ths sec */
  423.    }
  424. }
  425.  
  426. void cleanexit(s, err)
  427. UBYTE *s;
  428. int err;
  429. {
  430.    if(*s) printf(s);
  431.    cleanup();
  432.    exit(err);
  433. }
  434.  
  435. void cleanup()
  436. {
  437.    if(OpenedCon)  CDClose();
  438.       
  439.    if(window)
  440.    {
  441.       ClearMenuStrip(window);
  442.       CloseWindow(window);
  443.    }
  444.  
  445.    if(screen)
  446.    {
  447.       CloseScreen(screen);
  448.    }
  449.  
  450.    if(GfxBase)
  451.    {
  452.       CloseLibrary(GfxBase);
  453.    }
  454.  
  455.    if(IntuitionBase)
  456.    {
  457.       CloseLibrary(IntuitionBase);
  458.    }
  459. }
  460.  
  461. /*********************************************************************/
  462. /*                                                                   */
  463. /*  Program name:  cdio                                              */
  464. /*                                                                   */
  465. /*  Modified 04/14/88 by Carolyn Schepper - Commodore Amiga          */
  466. /*                                                                   */
  467. /*  Purpose:  To provide standard console device interface routines  */
  468. /*            such as Open, GetChar, PutChar, etc.                   */
  469. /*                                                                   */
  470. /*  Arguments:  (window)    - The window to associate the console    */
  471. /*                            device with. Used in CDOpen.           */
  472. /*              (character) - The char to output. Used in CDPutChar  */
  473. /*              (string)    - The string to output. Used in CDPutStr */
  474. /*                                                                   */
  475. /*********************************************************************/
  476.  
  477. struct IOStdReq *cwIO;
  478. struct IOStdReq *crIO;
  479. struct MsgPort  *cmPort;
  480.  
  481. UBYTE consoleReadChar  = 0;
  482. UBYTE OutChar = 0;
  483.  
  484. int CDOpen(window)
  485. struct Window *window;
  486. {
  487. int error;
  488.  
  489.    cwIO = 0L;
  490.    crIO = 0L;
  491.    cmPort = 0L;
  492.  
  493.    if(!(cmPort = (struct MsgPort *)CreatePort("myConPort",0)))
  494.    {
  495.       return(0);
  496.    }
  497.    if(!(cwIO = (struct IOStdReq *)CreateStdIO(cmPort)))
  498.    {
  499.       DeletePort(cmPort);
  500.       return(0);
  501.    }
  502.    if(!(crIO = (struct IOStdReq *)CreateStdIO(cmPort)))
  503.    {
  504.       DeleteStdIO(cwIO);
  505.       DeletePort(cmPort);
  506.       return(0);
  507.    }
  508.  
  509.    /* Open the console device for our window */
  510.    cwIO->io_Data = (APTR)window;
  511.    cwIO->io_Length = sizeof(*window);
  512.    if((error = OpenDevice("console.device", 0, cwIO, 0)))
  513.    {
  514.       printf("CDInit OpenDevice error: %d.\n", error);
  515.       return(0);
  516.    }
  517.  
  518.    /* Copy the initialized writeIO request to the readIO request */
  519.    *crIO = *cwIO;
  520.  
  521.    /* Send off the initial read request */
  522.    queue_read();
  523.    /* Return success */
  524.    return(1);
  525. }
  526.  
  527. void CDClose()
  528. {
  529.    if((cwIO)&&(cwIO->io_Device)) CloseDevice(cwIO);
  530.    if(cwIO)   DeleteStdIO(cwIO);
  531.    if(crIO)   DeleteStdIO(crIO);
  532.    if(cmPort) DeletePort(cmPort);
  533. }
  534.  
  535. int CDGetChar()
  536. {
  537. register temp;
  538.  
  539.    WaitPort(cmPort);
  540.    GetMsg(cmPort);
  541.    /* Copy char to temp before queue_read() which reuses it */
  542.    temp = consoleReadChar;
  543.    queue_read();
  544.    return(temp);
  545. }
  546.  
  547.  
  548. int CDMayGetChar()
  549. {
  550. register temp;
  551.  
  552.    if(!(CheckIO(crIO))) return(-1);
  553.    GetMsg(cmPort);
  554.    temp = consoleReadChar;
  555.    queue_read();
  556.    return(temp);
  557. }
  558.  
  559.  
  560. void queue_read()
  561. {
  562.    crIO->io_Command = CMD_READ;
  563.    crIO->io_Data = (APTR)&consoleReadChar;
  564.    crIO->io_Length = 1;
  565.    SendIO(crIO);
  566. }
  567.  
  568.  
  569. void CDPutChar(character)
  570. char character;
  571. {
  572.    OutChar = character;
  573.    cwIO->io_Command = CMD_WRITE;
  574.    cwIO->io_Data = (APTR)&OutChar;
  575.    cwIO->io_Length = 1;
  576.    DoIO(cwIO);
  577. }
  578.  
  579. void CDPutStr(string)
  580. char *string;                     /*  NULL terminated string to output */
  581. {
  582.    CDPutStrL(string,-1);
  583. }
  584.  
  585. void CDPutStrL(string,length)
  586. char *string;                     /* output string of length length    */
  587. int length;
  588. {
  589.    cwIO->io_Command = CMD_WRITE;
  590.    cwIO->io_Data = (APTR)string;
  591.    cwIO->io_Length = length;
  592.    DoIO(cwIO);
  593. }
  594.  
  595.